<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <?php
        session_start();
        include_once'bloco/head.php';
        include_once '../control/conexao.php';
        include_once '../control/valida_sessao.php';

        function __autoload($classe) {
            include_once"../model/{$classe}.class.php";
        }
        ?>
        <link href="bootstrap/css/morris.css" rel="stylesheet">
    </head>
    <body>      
        <div id="wrapper">
            <?php
            include_once'bloco/navbar.php';
            include_once '../control/valida_perfil.php';
            ?>
            <div id="page-wrapper">
                <?php include_once 'bloco/banner.php'; ?>                
                <div class="row">
                    <div class=" panel panel-success">
                        <div class="panel-heading">
                            <p class="panel-title"><i class = "glyphicon glyphicon-th-large"></i> Editar Empresa</p>
                        </div>
                        <div class="panel-body">
                            <div class="col-lg-12 ">
                                <form autocomplete="off" name="form_edita_empresa" method="post" action="../control/editar_empresa.php">
                                    <div class="row">
                                        <div class="col-lg-6 col-md-offset-3">
                                            <div class="alert alert-success alert-dismissable">
                                                <table class="table table-condensed table-responsive">                                    
                                                    <?php
                                                    $empresa = new empresa();
                                                    if ($_SESSION['usuario']->id_perfil == 4) {
                                                        $empresa->id_empresa = $_SESSION['usuario']->id_usuario;
                                                    } else {
                                                        $empresa->id_empresa = $_GET['id'];
                                                    }
                                                    $empresaDAO = new empresaDAO();
                                                    $empresa = $empresaDAO->consultar($empresa);

                                                    $usuario = new usuario();
                                                    $usuario->id_usuario = $empresa->id_empresa;
                                                    $usuarioDAO = new usuarioDAO();
                                                    $usuario = $usuarioDAO->consultar($usuario);
                                                    ?>
                                                    <tr>
                                                        <td><strong>Senha</strong></td>
                                                        <td><input type="password" name="senha" class="form-control" required value="<?php echo $usuario->usuario_senha ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Sigla / Nome Fantasia</strong></td>
                                                        <td><input type="text" name="sigla" class="form-control" required maxlength="20" value="<?php echo utf8_encode($empresa->empresa_sigla) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Razão Social</strong></td>
                                                        <td><input type="text" name="razao" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_razao) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>CNPJ</strong></td>
                                                        <?php
                                                        if ($_SESSION['usuario']->id_perfil == 0) {
                                                            ?>
                                                            <td><input type="text" id="cnpj" name="cnpj" class="form-control" required maxlength="17" value="<?php echo $empresa->empresa_cnpj ?>" onkeyup="mascara_cnpj(this)"/></td>
                                                            <?php
                                                        } else {
                                                            ?>
                                                            <td><input type="text" name="cnpj" class="form-control" required disabled value="<?php echo $empresa->empresa_cnpj ?>"/></td>
                                                        <input type="hidden" name="cnpj" value="<?php echo $empresa->empresa_cnpj ?>">
                                                    <?php } ?>
                                                    </tr>                                                    
                                                    <tr>
                                                        <td><strong>Endereço</strong></td>
                                                        <td><input type="text" name="endereco" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_endereco) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>CEP</strong></td>
                                                        <td><input type="text" id="cep" name="cep" class="form-control" required="" value="<?php echo $empresa->empresa_cep ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Bairro</strong></td>
                                                        <td><input type="text" name="bairro" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_bairro) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>E-mail</strong></td>
                                                        <td><input type="text" name="email" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_email) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Telefone</strong></td>
                                                        <td><input type="text" id="telefone" name="telefone" class="form-control" required="" value="<?php echo $empresa->empresa_telefone ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Nome do Responsável</strong></td>
                                                        <td><input type="text" name="nome_resp" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_nome_resp) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Cargo do Responsável</strong></td>
                                                        <td><input type="text" name="cargo_resp" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_cargo_resp) ?>"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td><strong>Especialização do Responsável</strong></td>
                                                        <td><input type="text" name="especializacao_resp" class="form-control" required="" value="<?php echo utf8_encode($empresa->empresa_especializacao_resp) ?>"/></td>
                                                    </tr>    

                                                    <?php
                                                    $consulta_estado = mysql_query("SELECT * FROM estados WHERE id_estado='$empresa->id_estado'");
                                                    $consulta_cidade = mysql_query("SELECT * FROM cidades WHERE id_cidade='$empresa->id_cidade'");
                                                    $estado = mysql_result($consulta_estado, 0, 'nome');
                                                    $cidade = mysql_result($consulta_cidade, 0, 'nome');

                                                    $res = mysql_query("SELECT * FROM estados ORDER BY sigla");
                                                    $num = mysql_num_rows($res);
                                                    while ($dados = mysql_fetch_array($res)) {
                                                        $id_estado[] = $dados['id_estado'];
                                                        $estado_nome[] = (utf8_encode($dados['nome']));
                                                    }
                                                    ?>
                                                    <tr>
                                                        <td>
                                                            <label>Estado:</label>
                                                        </td>
                                                        <td>
                                                            <select name="estado" id="estado" onchange="buscar_cidades()" class='btn btn-block'>                                
                                                                <option value="<?php echo $empresa->id_estado ?>"><?php echo ">>> " . utf8_encode($estado) . " <<<"; ?></option>
                                                                <?php
                                                                for ($i = 0; $i < $num; $i++) {
                                                                    ?>
                                                                    <option value="<?php echo $id_estado[$i] ?>"><?php echo utf8_encode($estado_nome[$i]) ?></option>
                                                                <?php } ?>                                
                                                            </select>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <label>Cidades:</label>
                                                        </td>
                                                        <td>
                                                            <select id="load_cidades" name="cidade" id="cidade" class='btn btn-block'>
                                                                <option value="<?php echo $empresa->id_cidade ?>"><?php echo ">>> " . utf8_encode($cidade) . " <<<"; ?></option>
                                                            </select>
                                                        </td>
                                                    </tr>                                    
                                                </table>
                                                <input type="hidden" name="id_usuario" value="<?php echo utf8_encode($usuario->id_usuario) ?>">
                                                <div align="center">
                                                    <input type="button" class="btn btn-inverse" value="Voltar" onclick="history.go(-1)"/>
                                                    <input type="submit" class="btn btn-success" value="Salvar"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        include_once'bloco/scripts.php';
        ?>        
        <script src="bootstrap/js/busca_cidades.js"></script>
        <script src="bootstrap/js/mask/mask.js"></script>
        <script>
                                                        jQuery(function($) {
                                                            $("#cnpj").mask("99.999999/9999-99");
                                                            $("#telefone").mask("(99) 9999-9999");
                                                            $("#cep").mask("99999-999");
                                                        });
        </script>
    </body>
</html>
